/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.askerov.dynamicgrid;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * The GridAdapter
 *
 * @since 2021-05-15
 */
public class GridAdapter {
    private static final int GRID_LAYOUT_BORDER_MARGIN = 24;
    private static final int GRID_ITEM_RIGHT_MARGIN = 8;

    private final List<Component> componentList = new ArrayList<>();

    /**
     * 适配器
     *
     * @param context
     * @param itemInfos
     * @param column
     */
    public GridAdapter(Context context, List<GridItemInfo> itemInfos,int column) {
        int itemPx = getItemWidthByScreen(context,column);
        for (GridItemInfo item : itemInfos) {
            Component gridItem = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_grid_item, null, false);
            gridItem.setTag(item.getTag());
            if (gridItem.findComponentById(ResourceTable.Id_grid_item_image) instanceof Image) {
                Image imageItem = (Image) gridItem.findComponentById(ResourceTable.Id_grid_item_image);
                imageItem.setPixelMap(item.getIconId());
            }
            if (gridItem.findComponentById(ResourceTable.Id_grid_item_text) instanceof Text) {
                Text textItem = (Text) gridItem.findComponentById(ResourceTable.Id_grid_item_text);
                textItem.setText(item.getItemText());
                textItem.setTextSize(item.getTextSize());
            }
            gridItem.setWidth(itemPx);
            gridItem.setHeight(itemPx);
            if (column > 6) {
                // 大于6列可能导致高度不够，文字显示不了下半部分，自动增加50高度
                gridItem.setHeight(itemPx + 50);
            }
            gridItem.setMarginRight(AttrHelper.vp2px(GRID_ITEM_RIGHT_MARGIN, context));
            componentList.add(gridItem);
        }
    }

    /**
     * method for get componentList
     *
     * @return componentList
     */
    public List<Component> getComponentList() {
        return componentList;
    }

    private int getItemWidthByScreen(Context context,int column) {
        int screenWidth = AppUtils.getScreenInfo(context).getPointXToInt();
        return (screenWidth
            - AttrHelper.vp2px(GRID_LAYOUT_BORDER_MARGIN, context) * 2
            - AttrHelper.vp2px(GRID_ITEM_RIGHT_MARGIN, context) * 3)
            / column;
    }
}
